package com.ffcs.cn.action.labelBase.workspace;

import com.supermap.data.Datasource;
import com.supermap.data.EngineType;
import com.supermap.desktop.controls.ui.controls.SmFileChoose;
import com.supermap.desktop.controls.utilities.DatasourceOpenUtilities;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.FileChooseMode;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.implement.CtrlAction;
import com.ffcs.cn.DevelopProperties;

import javax.swing.*;
import java.io.File;

import static com.supermap.desktop.core.utilties.DatasourceUtilities.getAllSupportFileExtensions;
import static com.supermap.desktop.core.utilties.DatasourceUtilities.getEngineSupportFileExtensions;

/**
 * 打开文件型数据源
 *
  */
public class CtrlActionDatasourceOpenFile extends CtrlAction {
	/**
	 * UDB文件描述
	 */
	private static final String UDBDescription = DevelopProperties.getString("String_FileFilters_Datasource_UDB");
	/**
	 * 影像文件描述
	 */
	private static final String ImageDescription = DevelopProperties.getString("String_FileFilters_ImagePlugins");
	/**
	 * 矢量文件描述
	 */
	private static final String VectorDescription = DevelopProperties.getString("String_FileFilters_Vector");

	public CtrlActionDatasourceOpenFile(IBaseItem caller) {
		super(caller);
	}

	@Override
	protected void run() {
		SmFileChoose datasourceChooser = new SmFileChoose("DatasourceOpenFile", FileChooseMode.OPEN_MANY)
				.setTitle(DevelopProperties.getString("String_Title_DatasourceOpenFile"))
				.addFileFilter(DevelopProperties.getString("String_FileFilters_all"), getAllSupportFileExtensions());

		datasourceChooser.addFileFilter(DevelopProperties.getString("String_FileFilters_Datasource_Spatialite"), "udbx+sqlite+gpkg");
		datasourceChooser.addFileFilter(DevelopProperties.getString("String_FileFilters_DCF"), "dcf");

		if (getEngineSupportFileExtensions(EngineType.UDB) != null && getEngineSupportFileExtensions(EngineType.UDB).length > 0) {
			datasourceChooser.addFileFilter(UDBDescription, "udb");
		}
		if (getEngineSupportFileExtensions(EngineType.IMAGEPLUGINS) != null && getEngineSupportFileExtensions(EngineType.IMAGEPLUGINS).length > 0) {
			datasourceChooser.addFileFilter(ImageDescription, getEngineSupportFileExtensions(EngineType.IMAGEPLUGINS));
		}
		if (getEngineSupportFileExtensions(EngineType.VECTORFILE) != null && getEngineSupportFileExtensions(EngineType.VECTORFILE).length > 0) {
			datasourceChooser.addFileFilter(VectorDescription, getEngineSupportFileExtensions(EngineType.VECTORFILE));
		}
		// 成功打开的个数
		int successCount = 0;
		if (datasourceChooser.showDefaultDialog() == JFileChooser.APPROVE_OPTION) {
			// 数据源文件
			File[] files = datasourceChooser.getSelectFiles();
			if (files != null) {
				for (File file : files) {
					// 打开文件型数据源
					Datasource datasource = DatasourceOpenUtilities.openFileDatasource(file);
					if (datasource != null) {
						successCount++;
					}
				}
				if (files.length > 0) {
					String message = String.format(DevelopProperties.getString("String_OpenDatasourceResultMsg"), files.length, successCount, files.length
							- successCount);
					Application.getActiveApplication().getOutput().output(message);
				}
			}
		}
	}
}
